#include "threadPool.h"
int func_mkdir(char *user_name,numStack_t *stack,char*fileName,MYSQL *mysql){
    if(stack==NULL)
    {
        //查询当前目录信息
        /*
        char sql1[]="SELECT * FROM file_excel";
        int ret = mysql_query(mysql,sql1);
        if(ret!=0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
            exit(-1);
        }
        */
        //插入数据库
        char sql2[1000]={0};
        sprintf(sql2,"INSERT INTO file_excel (file_name, user_name, parent_id, type, path, is_deleted, hash)"
                " VALUES ('/', '%s',-1, 'd', '/', 0, '0');"
                ,user_name);
        mysql_query(mysql,sql2);
        printf("在文件表中初始化用户根目录\n");

    }    

    //接受当前路径
    char cur_dir_path[50]={0};
    func_pwd(cur_dir_path,stack,mysql);
    //char new_dir_path[100]="/dir1/dir2";
    //查询当前目录信息
    char sql1[]="SELECT * FROM file_excel";
    int ret = mysql_query(mysql,sql1);
    if(ret!=0){
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        exit(-1);
    }
    //接受数据库表
    char id[100]={0};
    MYSQL_RES * rows =mysql_store_result(mysql);
    printf("col_num=%u,row_num=%lu\n",mysql_num_fields(rows),mysql_num_rows(rows));

    //接受每一行的内容
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(rows))) {
        if(strcmp(row[5],cur_dir_path)==0){
            //找到当前路径对应的id,id为插入数据中的父id
            strcpy(id,row[0]);
        }
    }
    //插入数据库
    
    char sql2[1000]={0};

    if(stack->stackSize==1)
    {
        
    sprintf(sql2,"INSERT INTO file_excel (file_name, user_name, parent_id, type, path, is_deleted, hash)"
            " VALUES ('%s', '%s',%s, 'd', '/%s', 0, '0');"                                             
            ,fileName,user_name,id,fileName);
    }
    else
    {

    sprintf(sql2,"INSERT INTO file_excel (file_name, user_name, parent_id, type, path, is_deleted, hash)"
            " VALUES ('%s', '%s',%s, 'd', '%s/%s', 0, '0');"                                             
            ,fileName,user_name,id,cur_dir_path,fileName);
    }
    mysql_query(mysql,sql2);
    mysql_free_result(rows);
    return 0;
}
